// pages/mine/members/members.js
const app = getApp(),
    globalData = app.globalData,
    api = require("../../../api/index"),
    utils = require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        backImg:globalData.pictureRouting+'wodetuanbeibackground.png',
        canpIcon:globalData.pictureRouting+'touxiang_icon_wodetiuand.png',
        activeNames: ['1'],
        // membertype：1 初级,2 高级,4 院长
        userInfo: {},
        status: 0,
        toast: {
            visible: false,
            text: ''
        },
        imageBaseUri: globalData.imageBaseUri,
        array: ['所有等级'],
        index: 0,
        filter: {
            NickName: '',
            MemberType: '',
            CurrenetPageIndex: 1,
            PageSize: 10
        },
        loading: false,
        hasMore: false,
        userData: {}
    },
    onChange(event){
        console.log(event)
        this.setData({
            activeNames: event.detail,
          });
          wx.navigateTo({
            url: '/pages/mine/teamDetails/index',
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.reload()
    },

    reload() {

        this.setData({
            userInfo: wx.getStorageSync('userinfo') || {}
        })
        
        if (this.data.userInfo.MemberGradeId == 1) {
            this.setData({
                array: ['所有等级', '产品顾问']
            })
        } else if (this.data.userInfo.MemberGradeId == 2) {
            this.setData({
                array: ['所有等级', '产品顾问', '高级产品顾问']
            })
        } else if (this.data.userInfo.MemberGradeId == 4) {
            this.setData({
                array: ['所有等级', '产品顾问', '高级产品顾问', '院长']
            })
        } else {
            this.setData({
                array: ['所有等级']
            })
        }

        this.getList();
        this.getMySales();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log(this.data.hasMore, 'hasmore')
        if (this.data.hasMore) {
            this.setData({
                'filter.CurrenetPageIndex': ++this.data.filter.CurrenetPageIndex
            });

            this.getList();
        }
    },

    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value
        })
        switch (this.data.array[this.data.index]) {
            case '产品顾问':
                this.setData({
                    'filter.MemberType': 1,
                    "filter.CurrenetPageIndex": 1
                })
                this.getList();
                break;
            case '高级产品顾问':
                this.setData({
                    'filter.MemberType': 2,
                    "filter.CurrenetPageIndex": 1
                })
                this.getList();
                break;
            case '院长':
                this.setData({
                    'filter.MemberType': 4,
                    "filter.CurrenetPageIndex": 1
                })
                this.getList();
                break;
            default:
                this.setData({
                    'filter.MemberType': '',
                     "filter.CurrenetPageIndex": 1
                })
                this.getList();
                return;
        }
        
    },

    // 获取所属会员列表
    async getList() {
        try {
            let res = await api.member.getChilds(this.data.filter),
                data = res.data.ResData || {},
                list = data.Items || [];
            console.log(res, '所属会员列表')

            //多页
            if (this.data.filter.CurrenetPageIndex > 1) {
                list = this.data.list.concat(list);
            }

            this.setData({
                status: 2,
                list,
                loading: false,
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
            })
        } catch (e) {
            console.log(e, '获取所属会员列表失败')
            this.setData({
                status: 1
            })
        }
    },

    // 获取本人的销售额
    async getMySales() {
        try {
            let res = await api.member.getSales();
            console.log(res,'本人销售额')
            this.setData({
                userData: res.data.ResData || {}
            })
        }catch(e){
            console.log(e,'获取本人销售额失败')
        }
    },

    // 输入
    getInput(e){
        console.log(e,'e')
        this.setData({
           "filter.NickName": e.detail.value
        })
    },
    search(){
        this.getList();
    }
})